//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FaceValueClaim.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/Claim.h>
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CFaceValueClaim::CFaceValueClaim (boost::shared_ptr<QuantLib::FaceValueClaim>& childNative, Object^ owner) : CClaim(CFaceValueClaim::typeid)
{
#ifdef HANDLE
	_phFaceValueClaim = NULL;
#endif
	_ppFaceValueClaim = &childNative;
    _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueClaim));
}
Cephei::QL::Instruments::CFaceValueClaim::CFaceValueClaim (QuantLib::FaceValueClaim& childNative, Object^ owner) : CClaim(CFaceValueClaim::typeid)
{
#ifdef HANDLE
	_phFaceValueClaim = NULL;
#endif
	_ppFaceValueClaim = new boost::shared_ptr<QuantLib::FaceValueClaim> (&childNative);
    _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueClaim));
    _FaceValueClaimOwner = owner;
    _ClaimOwner = owner;
}

Cephei::QL::Instruments::CFaceValueClaim::CFaceValueClaim (CFaceValueClaim^ copy) : CClaim(CFaceValueClaim::typeid)
{
#ifdef HANDLE
	_phFaceValueClaim = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFaceValueClaim = new boost::shared_ptr<QuantLib::FaceValueClaim> (copy->GetShared());
        _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueClaim));
    }
}
Cephei::QL::Instruments::CFaceValueClaim::CFaceValueClaim (PLATFORM::Type^ t) : CClaim(CFaceValueClaim::typeid)
{
#ifdef HANDLE
	_phFaceValueClaim = NULL;
#endif
	if (!t->IsSubclassOf(CFaceValueClaim::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CFaceValueClaim::CFaceValueClaim (QuantLib::Handle<QuantLib::FaceValueClaim>& childNative, Object^ owner)  : CClaim(CFaceValueClaim::typeid)
{
	_phFaceValueClaim = &childNative;
	_ppFaceValueClaim = &static_cast<boost::shared_ptr<QuantLib::FaceValueClaim>>(childNative.currentLink());
    _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueClaim));
    _FaceValueClaimOwner = owner;
}
Cephei::QL::Instruments::CFaceValueClaim::CFaceValueClaim (QuantLib::Handle<QuantLib::FaceValueClaim> childNative)  : CClaim(CFaceValueClaim::typeid)
{
	_phFaceValueClaim = &childNative;
	_ppFaceValueClaim = &static_cast<boost::shared_ptr<QuantLib::FaceValueClaim>>(childNative.currentLink());
    _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueClaim));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CFaceValueClaim::CFaceValueClaim (QuantLib::FaceValueClaim childNative)  : CClaim(CFaceValueClaim::typeid)
{
#ifdef HANDLE
	_phFaceValueClaim = NULL;
#endif
	_ppFaceValueClaim = new boost::shared_ptr<QuantLib::FaceValueClaim> (new QuantLib::FaceValueClaim (childNative));
    _ppClaim = new boost::shared_ptr<QuantLib::Claim> (boost::dynamic_pointer_cast<QuantLib::Claim> (*_ppFaceValueClaim));
}
#endif

Cephei::QL::Instruments::CFaceValueClaim::~CFaceValueClaim ()
{
    if (_ppFaceValueClaim != NULL)
    {
	    delete _ppFaceValueClaim;
        _ppFaceValueClaim = NULL;
    }
}
Cephei::QL::Instruments::CFaceValueClaim::!CFaceValueClaim ()
{
    if (_ppFaceValueClaim != NULL)
    {
	    delete _ppFaceValueClaim;
    }
}
QuantLib::FaceValueClaim& Cephei::QL::Instruments::CFaceValueClaim::GetReference ()
{
    if (_ppFaceValueClaim == NULL) throw REFNEW NativeNullException ();
	return **_ppFaceValueClaim;
}
boost::shared_ptr<QuantLib::FaceValueClaim>& Cephei::QL::Instruments::CFaceValueClaim::GetShared ()
{
    if (_ppFaceValueClaim == NULL) throw REFNEW NativeNullException ();
	return *_ppFaceValueClaim;
}
QuantLib::FaceValueClaim* Cephei::QL::Instruments::CFaceValueClaim::GetPointer ()
{
    if (_ppFaceValueClaim == NULL) throw REFNEW NativeNullException ();
	return &**_ppFaceValueClaim;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FaceValueClaim>& Cephei::QL::Instruments::CFaceValueClaim::GetHandle ()
{
	if (_phFaceValueClaim == NULL)
	{
		_phFaceValueClaim = new Handle<QuantLib::FaceValueClaim> (*_ppFaceValueClaim);
	}
	return *_phFaceValueClaim;
}
#endif
bool Cephei::QL::Instruments::CFaceValueClaim::HasNative () 
{
	return (_ppFaceValueClaim != NULL);
}

Double Cephei::QL::Instruments::CFaceValueClaim::Amount (DateTime d, Double notional, Double recoveryRate)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
        QuantLib::Real _notional = (QuantLib::Real)ValueHelper::Convert (notional); //a
        QuantLib::Real _recoveryRate = (QuantLib::Real)ValueHelper::Convert (recoveryRate); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFaceValueClaim)->amount ( _d,  _notional,  _recoveryRate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

